using System.Collections.Generic;
using System.Configuration;
using System.Linq;

namespace Common.Configuration.Listener
{
    public class ListenerConfig : ConfigurationSection, IListenerConfig
    {
        private const string PrefixesPropertyName = "Prefixes";
        private const string MaxConcurentRequestsPropertyName = "MaximumConcurentRequests";
        protected static string SectionName { get { return "ListenerConfig"; } }

        public static ListenerConfig Load()
        {
            var cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            var section = cfg.GetSection(SectionName);
            return (ListenerConfig) section;
        }

        [ConfigurationProperty(PrefixesPropertyName)]
        public PrefixCollection PrefixCollection
        {
            get { return (PrefixCollection)this[PrefixesPropertyName]; }
            set { this[PrefixesPropertyName] = value; }
        }

        public IList<string> Prefixes { get { return PrefixCollection.Cast<PrefixElement>().Select(p => p.FullValue).ToArray(); } }

        [ConfigurationProperty(MaxConcurentRequestsPropertyName)]
        public int MaximumConcurentRequests
        {
            get { return (int)this[MaxConcurentRequestsPropertyName]; }
            set { this[MaxConcurentRequestsPropertyName] = value; }
        }
    }
}